import { defineStore } from 'pinia'
import { store } from '../index'
import { parse, stringify } from 'zipson'
import {
  OutputFormat,
  Resolution,
  Bitrate,
  Framerate,
  AudioQuality,
  VideoCodec,
  VideoState
} from '@renderer/enums/settingEnum'
import { VideoType } from '@renderer/types/store'

interface SettingsState {
  // 转换视频列表
  files: VideoType[]
  // 文件输出位置
  outSavePath: string
  // 默认输出格式
  defaultFormat: OutputFormat
  // 分辨率
  resolution: Resolution
  // 码率
  bitrate: Bitrate
  // 帧率
  framerate: Framerate
  // 音频质量
  audioQuality: AudioQuality
  // 默认视频编码器
  videoCodec: VideoCodec
  // 同时处理的文件数量
  maxProcessCount: number
}

export const useSettingsStore = defineStore('settingsStore', {
  state: (): SettingsState => ({
    files: [],
    outSavePath: '',
    defaultFormat: OutputFormat.MP4,
    resolution: Resolution.FHD,
    bitrate: Bitrate.MEDIUM,
    framerate: Framerate.MEDIUM,
    audioQuality: AudioQuality.MEDIUM,
    videoCodec: VideoCodec.H264,
    maxProcessCount: 3
  }),
  getters: {
    // 可以根据需要添加一些计算属性
  },
  actions: {
    // 暂停
    pauseTransform(path: string) {
      this.files.map((file) => {
        if (file.path === path) file.state = VideoState.PENDING
      })
    },
    // 恢复
    resumeTransform(path: string) {
      this.files.map((file) => {
        if (file.path === path) file.state = VideoState.CONVERTING
      })
    },
    // 转换完成
    transformComplete(path: string) {
      this.files.map((file) => {
        if (file.path === path) {
          file.progress = 100
          file.state = VideoState.SUCCESS
        }
      })
    },
    // 删除单个文件
    removeFile(path: string) {
      this.files = this.files.filter((file) => file.path !== path)
    },
    // 清空所有文件
    clearFiles() {
      this.files = []
    }
  },
  persist: {
    key: 'settings',
    storage: localStorage,
    serializer: {
      deserialize: parse,
      serialize: stringify
    }
  }
})

// 非setup
export function useSettingsStoreHook() {
  return useSettingsStore(store)
}
